package com.gzhu.knowledgeAdmin.controller;

import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.utils.GoFastDfsUtil;
import com.gzhu.knowledgeAdmin.common.vo.GoFastDfsUploadFileVo;
import com.gzhu.knowledgeAdmin.service.OssService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

/**
 * @author laoba
 * @create 2023-04-28 17:58
 */
@RestController
@RequestMapping("/file")
@Api(tags = "OSS控制器，图片的处理")
public class OssController {


    @Autowired
    private OssService ossService;

    @Autowired
    private GoFastDfsUtil goFastDfsUtil;

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file) throws Exception {
        GoFastDfsUploadFileVo uploadFileVo = goFastDfsUtil.uploadFile(file);
        return R.ok(uploadFileVo);

    }

    /**
     * 根据路径删除文件
     * @param url
     * @return
     */
    @GetMapping("/deleleFile")
    public R deleleFile(@RequestParam String url) throws MalformedURLException {
        goFastDfsUtil.deleteFileByUrl(url);
        return R.ok();
    }

    /**
     * 根据文件URL下载文件
     * @param url
     * @param response
     * @throws MalformedURLException
     */
    @GetMapping("/downloadFile")
    public void downloadFile(@RequestParam String url, HttpServletResponse response) throws MalformedURLException, URISyntaxException {
        goFastDfsUtil.downloadFile(url,response);
    }
}
